package com.whu.MenuHub.controller;


import com.whu.MenuHub.domain.ReceivingAddress;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/address")
@Api(tags = "地址信息管理API")
public class AddressController {
    @Autowired
    AddressService addressService;

    @GetMapping("/userID/{userID}")
    @ApiOperation("根据用户id获取收获地址信息")
    @PreAuthorize("hasAnyAuthority(#userID)")
    public List<ReceivingAddress> getaddressByUserID(@ApiParam("用户ID")@PathVariable Integer userID){
        return addressService.getaddressByUserID(userID);
    }

    @PostMapping("")
    @ApiOperation("新增一条收获地址信息")
    @PreAuthorize("hasAnyAuthority(#address.userID)")
    public ReceivingAddress InsertAddress(@ApiParam("地址信息")@RequestBody ReceivingAddress address) throws MenuHubException {
        //判断用户已经有地址，如果没有则设置地址为为默认地址
        List<ReceivingAddress> addresses=addressService.getaddressByUserID(address.getUserID());
        if(addresses==null){
            address.setIsDefaultAddress(true);
        }
        if(address.getIsDefaultAddress()){
            //先获取到原定的默认地址，判断是否存在，若存在，跟现在这条记录是否相同
            ReceivingAddress tmpAddress=addressService.getDefaultAddress(address.getUserID());
            if(tmpAddress!=null&&tmpAddress.getId()!=address.getId()){
                //如果存在，且记录的id不相同，则取消原来的默认地址
                tmpAddress.setIsDefaultAddress(false);
                addressService.updateAddress(tmpAddress);
            }
        }
        return addressService.insertAddress(address);
    }

    @PutMapping("")
    @ApiOperation("修改一条收获地址信息")
    @PreAuthorize("hasAnyAuthority(#address.userID)")
    public void updateAddress(@ApiParam("地址信息")@RequestBody ReceivingAddress address) throws MenuHubException {
       //如果要更新为默认地址
        if(address.getIsDefaultAddress()){
            //先获取到原定的默认地址，判断是否存在，若存在，跟现在这条记录是否相同
            ReceivingAddress tmpAddress=addressService.getDefaultAddress(address.getUserID());
            if(tmpAddress!=null&&tmpAddress.getId()!=address.getId()){
                //如果存在，且记录的id不相同，则取消原来的默认地址
                tmpAddress.setIsDefaultAddress(false);
                addressService.updateAddress(tmpAddress);
            }
        }
        addressService.updateAddress(address);
    }

    @DeleteMapping("/{id}/{userID}")
    @ApiOperation("删除一条收获地址信息")
    @PreAuthorize("hasAnyAuthority(#userID)")
    public void deleteAddress(@ApiParam("地址ID")@PathVariable Integer id,
                                                 @ApiParam("用户ID")@PathVariable Integer userID) throws MenuHubException {
        ReceivingAddress address=addressService.getAddressByID(id);
        addressService.deleteAddressById(id,userID);
        //如果删除的是默认地址
        if(address.getIsDefaultAddress()){
            List<ReceivingAddress>addressList=addressService.getaddressByUserID(userID);
            //找用户的第一条地址，设置为默认值
            if(addressList!=null){
                ReceivingAddress tmpAddress=addressList.get(0);
                tmpAddress.setIsDefaultAddress(true);
                addressService.updateAddress(tmpAddress);
            }
        }
    }
}
